package com.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.service.ISMSCenterService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

@Service("SMSCenterService")
public class SMSCenterService implements ISMSCenterService {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 向手机发送注册验证码
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年7月12日
	 */
	@Override
	public String registerSend(String phoneNo) {
		Boolean isSuccess = true;
		Map<String, Object> map = getSMS_setting("register");
		if (StringUtils.isEmpty(phoneNo) || map.size() < 0) {
			return "false";
		}
		StringBuffer codeBuffer = new StringBuffer();
		for(int i=0;i<6;i++){
			codeBuffer.append((int)(Math.random() * 10));
		}
		AlibabaAliqinFcSmsNumSendResponse rsp;
		TaobaoClient client = null;
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		try {
			// TODO 调用第三方服务发送手机验证码
			String url = map.get("url").toString();
			String appkey = map.get("appkey").toString();
			String secret = map.get("secret").toString();
			Object product = map.get("product").toString();
			Object signNameObj = map.get("signName");
			String signName = "大鱼测试";
			if (signNameObj != null) {
				signName = signNameObj.toString();
			}
			client = new DefaultTaobaoClient(url, appkey, secret);
			req.setExtend("123456");
			req.setSmsType("normal");
			req.setSmsFreeSignName(signName);
			req.setSmsParamString("{\"code\":\""+codeBuffer.toString()+"\",\"product\":\""+product+"\"}");
			/*Pattern compile = Pattern.compile("[,，]");
			String[] split = compile.split(phoneNo);*/
			//String[] split = phoneNo.split("|");
			req.setRecNum(phoneNo);
			req.setSmsTemplateCode(map.get("templateCode").toString());
			rsp = client.execute(req);
		} catch (ApiException e) {
			isSuccess = false;
			e.printStackTrace();
		} catch (Exception e) {
			isSuccess = false;
			e.printStackTrace();
			// TODO: handle exception
		}
		return isSuccess ? codeBuffer.toString() : isSuccess.toString();
	}
	/**
	 * 向手机发送调度通知
	 * @param phoneNos 一次调用最多传入200个号码
	 * @param name
	 * @param time
	 * @param place
	 * @param action
	 * @return
	 * @author ServerZhang
	 * @date 2016年9月5日
	 */
	@Override
	public Boolean dispatchSend(String phoneNos,String name,
			String time,String place,String action) {
		Boolean isSuccess = true;
		Map<String, Object> map = getSMS_setting("dispatch");
		if (StringUtils.isEmpty(phoneNos) || map.size() < 0 
				|| phoneNos.split(",").length > 200) {
			return false;
		}
		AlibabaAliqinFcSmsNumSendResponse rsp;
		TaobaoClient client = null;
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		try {
			// TODO 调用第三方服务发送手机验证码
			String url = map.get("url").toString();
			String appkey = map.get("appkey").toString();
			String secret = map.get("secret").toString();
			Object product = map.get("product").toString();
			Object signNameObj = map.get("signName");
			String signName = "大鱼测试";
			if (signNameObj != null) {
				signName = signNameObj.toString();
			}
			client = new DefaultTaobaoClient(url, appkey, secret);
			req.setExtend("123456");
			req.setSmsType("normal");
			req.setSmsFreeSignName(signName);
			StringBuffer smsBuffer = new StringBuffer();
			smsBuffer.append("{\"name\":\"").append(name)
				.append("\",\"time\":\"").append(time)
				.append("\",\"place\":\"").append(place)
				.append("\",\"act\":\"").append(action)
				.append("\",\"product\":\"").append(product)
				.append("\"}");
			req.setSmsParamString(smsBuffer.toString());
			req.setRecNum(phoneNos);
			req.setSmsTemplateCode(map.get("templateCode").toString());
			rsp = client.execute(req);
		} catch (ApiException e) {
			isSuccess = false;
			e.printStackTrace();
		} catch (Exception e) {
			isSuccess = false;
			e.printStackTrace();
			// TODO: handle exception
		}
		return isSuccess;
	}
	/**
	 * 向手机发送占用场地通知
	 * @param phoneNos 一次调用最多传入200个号码
	 * @param name
	 * @param time
	 * @param place
	 * @param action
	 * @return
	 * @author ServerZhang
	 * @date 2016年9月7日
	 */
	@Override
	public Boolean needPlaceSend(String phoneNos,String name,
			String time,String place,String action) {
		Boolean isSuccess = true;
		Map<String, Object> map = getSMS_setting("place");
		if (StringUtils.isEmpty(phoneNos) || map.size() < 0 
				|| phoneNos.split(",").length > 200) {
			return false;
		}
		AlibabaAliqinFcSmsNumSendResponse rsp;
		TaobaoClient client = null;
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		try {
			// TODO 调用第三方服务发送手机验证码
			String url = map.get("url").toString();
			String appkey = map.get("appkey").toString();
			String secret = map.get("secret").toString();
			Object product = map.get("product").toString();
			Object signNameObj = map.get("signName");
			String signName = "大鱼测试";
			if (signNameObj != null) {
				signName = signNameObj.toString();
			}
			client = new DefaultTaobaoClient(url, appkey, secret);
			req.setExtend("123456");
			req.setSmsType("normal");
			req.setSmsFreeSignName(signName);
			StringBuffer smsBuffer = new StringBuffer();
			smsBuffer.append("{\"name\":\"").append(name)
			.append("\",\"time\":\"").append(time)
			.append("\",\"place\":\"").append(place)
			.append("\",\"act\":\"").append(action)
			.append("\"}");
			req.setSmsParamString(smsBuffer.toString());
			req.setRecNum(phoneNos);
			req.setSmsTemplateCode(map.get("templateCode").toString());
			rsp = client.execute(req);
		} catch (ApiException e) {
			isSuccess = false;
			e.printStackTrace();
		} catch (Exception e) {
			isSuccess = false;
			e.printStackTrace();
			// TODO: handle exception
		}
		return isSuccess;
	}
	
	private Map<String, Object> getSMS_setting(String type){
		if (StringUtils.isEmpty(type)) {
			return new HashMap<String, Object>();
		}
		String sql = "SELECT * FROM education.smssetting where type='%s' order by id desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, type));
		if (list.size() > 0) {
			return list.get(0);
		}else {
			return new HashMap<String, Object>();
		}
	}
}
